<?php

require_once( "$IP/includes/Title.php" );

if ( ! defined( 'MEDIAWIKI' ) )
	die( 1 );


/**
 * @todo document
 */
class SoundshareTitle extends Title 
{
	var $mHasData = false;

	var $mArtist = null;
	var $mArtistUrl = null;
	var $mArtistKey = null;
	var $mNoArtist = null;
	var $mNoArtistUrl = null;
	var $mNoArtistKey = null;

	var $mArtistDisplay = null;
	var $mNoArtistDisplay = null;
	var $mDisplay = null;

	public function getDisplayInfo()
	{
		switch ($this->mNamespace)
		{
			case NS_RELEASE:
			case NS_SONG:	
				$sql = " union select type, display from ss_tags_info WHERE type={$this->mNamespace} and artistKey='{$this->mArtistKey}' and noArtistKey='{$this->mNoArtistKey}'";
			case NS_ARTIST:
				$sql = "select type, display FROM ss_tags_info WHERE type=".NS_ARTIST." and artistKey='{$this->mArtistKey}'$sql";
				
				$dbr = wfGetDB( DB_SLAVE );
        		       	$res = $dbr->query($sql);
				while ($info = $dbr->fetchObject($res))
				{
					if ($info->type==NS_ARTIST)
						$this->mArtistDisplay = $info->display;
					else
						$this->mNoArtistDisplay = $info->display;
				}
				$dbr->freeResult($res);
				break;
		}
	}	

	public function getDisplay()
	{
		if ($this->mDisplay!=null) return $this->mDisplay;

		switch ($this->mNamespace)
		{
			case NS_RELEASE:
			case NS_SONG:	
				return coalesce($this->mArtistDisplay, $this->mArtist)." - ".coalesce($this->mNoArtistDisplay, $this->mNoArtist);
				
			case NS_ARTIST:
				return coalesce($this->mArtistDisplay, $this->mArtist);

			default:
				return urldecode($this->mTextform);
		}
	}

	/**
	 * Get the main part with underscores
	 * @return \type{\string} Main part of the title, with underscores
	 */
	public function getDBkey() {
		global $wgLang;
		switch ($this->mNamespace)
		{ 
			case NS_ARTIST:
			case NS_SONG:
			case NS_RELEASE:
		 	case NS_CATEGORY:
				return normalizeUrl($this->mDbkeyform, $this->mNamespace).'/'. $wgLang->getCode();
			
			default:
				return parent::getDBkey();
		}
	}

	public function parsePageTitle($ns, $title)
	{
		switch ($ns)
		{
			case NS_ARTIST:
			case NS_SONG:
			case NS_RELEASE:
			case NS_CATEGORY:
				$titlelen = strlen($title);
				if (substr($title, $titlelen-3, 1)=='/') $title = substr($title, 0, $titlelen-3);
				break;
		}
		return $title;
	}

	public function loadSoundshareData() 
	{
		global $wgRequest;

		if ($this->mHasData) return;
		$this->mHasData = true;
		switch ($this->mNamespace)
		{
			case NS_ARTIST:
				$this->mArtistUrl = $this->mTextform; 
				$this->mArtist = urldecode($this->mArtistUrl);
				$this->mArtistKey = normalize($this->mArtist);
				break;
			case NS_SONG:
			case NS_RELEASE:
				$info = explode("/", $this->mTextform, 2);
				$this->mArtistUrl = $info[0];
				$this->mNoArtistUrl = $info[1];
				$this->mArtist = urldecode($this->mArtistUrl);
				$this->mNoArtist = urldecode($this->mNoArtistUrl); 
				$this->mArtist." - ".$this->mNoArtist;
				$this->mArtistKey = normalize($this->mArtist);
				$this->mNoArtistKey = normalize($this->mNoArtist);
				break;
		}
	}
		
	public function secureAndSplit() {
		$this->mDbkeyform = str_replace(array('<', '[', '{'), '(', $this->mDbkeyform);
		$this->mDbkeyform = str_replace(array('>', ']', '}'), ')', $this->mDbkeyform);
		$this->mDbkeyform = str_replace(array('|', '#', ';'), ' ', $this->mDbkeyform);
		$dbkey = $this->mDbkeyform;
		$urlOk = parent::secureAndSplit();
		if (!$urlOk){
			$this->mDbkeyform = normalizeUrl($dbkey, $this->mNamespace);
			$urlOk = parent::secureAndSplit();
		}
		$this->loadSoundshareData();
		return true;
	}
	
	public function getLocalURL( $query = '', $variant = false ) {
		$this->loadSoundshareData();
		switch ($this->mNamespace)
		{
			case NS_ARTIST:
				$url = createURL(URL_ARTIST, array($this->mArtistUrl));
				break;
			case NS_SONG:
				$url = createURL(URL_SONG, array($this->mArtistUrl, $this->mNoArtistUrl));
				break;
			case NS_RELEASE:
				$url = createURL(URL_RELEASE, array($this->mArtistUrl, $this->mNoArtistUrl));
				break;
			case NS_CATEGORY:
				$url = createURL(URL_GENRE, array($this->mDbkeyform));
				break;
			case NS_USER:
				$url = createURL(URL_USER, array($this->mDbkeyform));
				break;
			case NS_SPECIAL:
				if (strncasecmp ( $this->mDbkeyform, substr(URL_SEARCH, 1), 7)==0) {
					$url = createURL(URL_SEARCH, array(substr($this->mDbkeyform, 7)));
					break;
				} else {
					$url = createURL(URL_SPECIAL, array($this->mDbkeyform));
				}
				break;
			default:
				return parent::getLocalURL($query, $variant);
				break;
		}
		if( is_array( $query ) ) $query = wfArrayToCGI( $query );
		if ($query!='') $url .= "?$query";
		return $url;
	}

}
